﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
	public class listLocationsOutput : Api_BaseOutput
	{
		public List<listLocationsResults> result { get; set; }
		public listLocationsParms inputParms { get; private set; }
		//
		public listLocationsOutput( listLocationsInput input )
			: base( input )
		{
			inputParms = input.parms;
			result = new List<listLocationsResults>();
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return string.Format( "List: {0}, locations returned: {1}",
				this.inputParms.id, result.Count );
		}

		#region ================================================================== format setup
		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}
		// ---- xmlrpc
		internal void Format( x_listLocations[] rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}
		#endregion

		#region  ===================================================================== format result
		// ---------------------------------------------------------- outputXML
		protected void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			// error check
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// -------------------------------------------- get values
			try
			{
				IEnumerable<XElement> activities = xml.Elements( "struct" );
				foreach ( XElement item in activities )
				{
					listLocationsResults entry = new listLocationsResults();
					double percent = 0; double total = 0;
					// ----------------------------------
					entry.country = item.Element( "country" ).Value ?? "";
					entry.countrycode = item.Element( "cc" ).Value ?? "";
					double.TryParse( item.Element( "percent" ).Value, out percent );
					entry.percent = percent;
					double.TryParse( item.Element( "total" ).Value, out total );
					entry.total = total;
					//------------------- 
					this.result.Add( entry );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// ---------------------------------------------------------- outputJSON
		protected void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();

			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					x_listLocations[] jsonResult = jss.Deserialize<x_listLocations[]>( this.api_Response );
					this.result = FormatResults( jsonResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// -----------------------------------------------------
		protected List<listLocationsResults> FormatResults( x_listLocations[] x_results )
		{
			List<listLocationsResults> results = new List<listLocationsResults>();
			//
			foreach ( x_listLocations item in x_results )
			{
				listLocationsResults entry = new listLocationsResults();
				// ----------------------------------
				entry.country = item.country ?? "";
				entry.countrycode = item.cc ?? "";
				entry.percent = item.percent;
				entry.total = item.total;
				//------------------- 
				results.Add( entry );
			}
			return results;
		}
		//
		#endregion
	}
}
